package net.luoran.dvm.common.util;

import net.luoran.dvm.entity.AuthInfo;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;


/**
 * @author ：M1nG
 * @date ：Created in 2020/11/11 22:07
 * @description：
 * @version: 1.0
 */
public class AuthUtil {

    public static final String TOKEN_KEY = "dvsAuthToken";

    public static AuthInfo Token2AuthInfo(String token) {
        return new AuthInfo().init(new CiphertextUtil().decrypt(token));
    }

    public static String AuthInfo2Token(AuthInfo authInfo) {
        return new CiphertextUtil().encrypt(authInfo.toStr());
    }

    /**
     * 校验token是否正确
     *
     * @param token 令牌
     * @return 是否正确
     */
    public static boolean verify(String token) {
        try {
            AuthInfo authInfo = Token2AuthInfo(token);
            LocalDateTime expireTime = LocalDateTime.parse(authInfo.getExpireTime(), DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
            return !LocalDateTime.now().isAfter(expireTime);
        } catch (Exception e) {
            return false;
        }
    }

}
